package net.xxcxcxcx.xc_enhancedmob.enhanced.rarity;

import net.minecraft.entity.attribute.EntityAttribute;
import net.minecraft.entity.attribute.EntityAttributeInstance;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import net.xxcxcxcx.xc_enhancedmob.component.EnhancedBaseComponent;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static net.xxcxcxcx.xc_enhancedmob.EnhancedMobMod.StartTest;

/**
 * 生物稀有度词条
 */
public enum Rarity {
    Unknown(MakeAttributeslist(0), MakeAttributeModifierlist(0), getText(0)),
    Ordinary(MakeAttributeslist(1), MakeAttributeModifierlist(1), getText(1)),
    Good(MakeAttributeslist(2), MakeAttributeModifierlist(2), getText(2)),
    Outstanding(MakeAttributeslist(3), MakeAttributeModifierlist(3), getText(3)),
    Excellent(MakeAttributeslist(4), MakeAttributeModifierlist(4), getText(4)),
    Ingenuity(MakeAttributeslist(5), MakeAttributeModifierlist(5), getText(5)),
    Epic(MakeAttributeslist(6), MakeAttributeModifierlist(6), getText(6)),
    Legend(MakeAttributeslist(7), MakeAttributeModifierlist(7), getText(7));
    private final List<EntityAttribute> Attributeslist;//生物强化列表
    private final List<EntityAttributeModifier> AttributeModifierlist;//生物强化列表对应的属性
    private final Text text;

    Rarity(List<EntityAttribute> list1, List<EntityAttributeModifier> list2, Text t) {
        Attributeslist = list1;
        AttributeModifierlist = list2;
        text = t;
    }

    //初始化生物强化列表
    private static List<EntityAttribute> MakeAttributeslist(int i) {
        List<EntityAttribute> list = new ArrayList<>();
        switch (i) {
            case 0, 1, 2, 3, 4, 5, 6, 7 -> {
                list.add(EntityAttributes.GENERIC_MAX_HEALTH);//最大生命值
                list.add(EntityAttributes.GENERIC_ARMOR);//盔甲值
                list.add(EntityAttributes.GENERIC_ARMOR_TOUGHNESS);//盔甲韧性
                list.add(EntityAttributes.GENERIC_ATTACK_DAMAGE);//攻击伤害
                list.add(EntityAttributes.GENERIC_ATTACK_KNOCKBACK);//攻击击退距离
                list.add(EntityAttributes.GENERIC_ATTACK_SPEED);//攻速
                list.add(EntityAttributes.GENERIC_MOVEMENT_SPEED);//移动速度
                list.add(EntityAttributes.GENERIC_KNOCKBACK_RESISTANCE);//击退抗性
                list.add(EntityAttributes.GENERIC_FOLLOW_RANGE);//索敌距离
            }
        }
        return list;
    }

    //初始化生物强化列表对应的属性
    private static List<EntityAttributeModifier> MakeAttributeModifierlist(int i) {
        List<EntityAttributeModifier> list = new ArrayList<>();
        switch (i) {
            case 0 -> {
                list.add(new EntityAttributeModifier("Unknown_MAX_HEALTH_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Unknown_ARMOR_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Unknown_ARMOR_TOUGHNESS_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Unknown_ATTACK_DAMAGE_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Unknown_ATTACK_KNOCKBACK_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Unknown_ATTACK_SPEED_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Unknown_MOVEMENT_SPEED_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Unknown_KNOCKBACK_RESISTANCE_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Unknown_FOLLOW_RANGE_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
            }
            case 1 -> {
                list.add(new EntityAttributeModifier("Ordinary_MAX_HEALTH_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Ordinary_ARMOR_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Ordinary_ARMOR_TOUGHNESS_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Ordinary_ATTACK_DAMAGE_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Ordinary_ATTACK_KNOCKBACK_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Ordinary_ATTACK_SPEED_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Ordinary_MOVEMENT_SPEED_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Ordinary_KNOCKBACK_RESISTANCE_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Ordinary_FOLLOW_RANGE_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
            }
            case 2 -> {
                list.add(new EntityAttributeModifier("Good_MAX_HEALTH_BOOST", 1, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Good_ARMOR_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Good_ARMOR_TOUGHNESS_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Good_ATTACK_DAMAGE_BOOST", 0.1, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Good_ATTACK_KNOCKBACK_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Good_ATTACK_SPEED_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Good_MOVEMENT_SPEED_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Good_KNOCKBACK_RESISTANCE_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Good_FOLLOW_RANGE_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
            }
            case 3 -> {
                list.add(new EntityAttributeModifier("Outstanding_MAX_HEALTH_BOOST", 3, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Outstanding_ARMOR_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Outstanding_ARMOR_TOUGHNESS_BOOST", 0.5, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Outstanding_ATTACK_DAMAGE_BOOST", 0.5, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Outstanding_ATTACK_KNOCKBACK_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Outstanding_ATTACK_SPEED_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Outstanding_MOVEMENT_SPEED_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Outstanding_KNOCKBACK_RESISTANCE_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Outstanding_FOLLOW_RANGE_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
            }
            case 4 -> {
                list.add(new EntityAttributeModifier("Excellent_MAX_HEALTH_BOOST", 7, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Excellent_ARMOR_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Excellent_ARMOR_TOUGHNESS_BOOST", 1, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Excellent_ATTACK_DAMAGE_BOOST", 1, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Excellent_ATTACK_KNOCKBACK_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Excellent_ATTACK_SPEED_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Excellent_MOVEMENT_SPEED_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Excellent_KNOCKBACK_RESISTANCE_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Excellent_FOLLOW_RANGE_BOOST", 0.5, EntityAttributeModifier.Operation.fromId(1)));
            }
            case 5 -> {
                list.add(new EntityAttributeModifier("Ingenuity_MAX_HEALTH_BOOST", 15, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Ingenuity_ARMOR_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Ingenuity_ARMOR_TOUGHNESS_BOOST", 2, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Ingenuity_ATTACK_DAMAGE_BOOST", 2, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Ingenuity_ATTACK_KNOCKBACK_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Ingenuity_ATTACK_SPEED_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Ingenuity_MOVEMENT_SPEED_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Ingenuity_KNOCKBACK_RESISTANCE_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Ingenuity_FOLLOW_RANGE_BOOST", 1, EntityAttributeModifier.Operation.fromId(1)));
            }
            case 6 -> {
                list.add(new EntityAttributeModifier("Epic_MAX_HEALTH_BOOST", 31, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Epic_ARMOR_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Epic_ARMOR_TOUGHNESS_BOOST", 3, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Epic_ATTACK_DAMAGE_BOOST", 3, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Epic_ATTACK_KNOCKBACK_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Epic_ATTACK_SPEED_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Epic_MOVEMENT_SPEED_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Epic_KNOCKBACK_RESISTANCE_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Epic_FOLLOW_RANGE_BOOST", 2, EntityAttributeModifier.Operation.fromId(1)));
            }
            case 7 -> {
                list.add(new EntityAttributeModifier("Legend_MAX_HEALTH_BOOST", 63, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Legend_ARMOR_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Legend_ARMOR_TOUGHNESS_BOOST", 4, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Legend_ATTACK_DAMAGE_BOOST", 4, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Legend_ATTACK_KNOCKBACK_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Legend_ATTACK_SPEED_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Legend_MOVEMENT_SPEED_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Legend_KNOCKBACK_RESISTANCE_BOOST", 0, EntityAttributeModifier.Operation.fromId(1)));
                list.add(new EntityAttributeModifier("Legend_FOLLOW_RANGE_BOOST", 3, EntityAttributeModifier.Operation.fromId(1)));
            }
        }
        return list;
    }

    //获取对应文本生成
    @Nullable
    private static Text getText(int i) {
        //translatableText.setStyle(translatableText.getStyle().withColor(TextColor.fromRgb(123456)));
        TranslatableText translatableText = null;
        switch (i) {
            case 0 -> {
                translatableText = new TranslatableText("enhanced.rarity.Unknown");
                translatableText.formatted(Formatting.RED);
            }
            case 1 -> {
                translatableText = new TranslatableText("enhanced.rarity.Ordinary");
                translatableText.formatted(Formatting.GRAY);
            }
            case 2 -> {
                translatableText = new TranslatableText("enhanced.rarity.Good");
                translatableText.formatted(Formatting.WHITE);
            }
            case 3 -> {
                translatableText = new TranslatableText("enhanced.rarity.Outstanding");
                translatableText.formatted(Formatting.GREEN);
            }
            case 4 -> {
                translatableText = new TranslatableText("enhanced.rarity.Excellent");
                translatableText.formatted(Formatting.BLUE);
            }
            case 5 -> {
                translatableText = new TranslatableText("enhanced.rarity.Ingenuity");
                translatableText.formatted(Formatting.DARK_PURPLE);
            }
            case 6 -> {
                translatableText = new TranslatableText("enhanced.rarity.Epic");
                translatableText.formatted(Formatting.GOLD);
            }
            case 7 -> {
                translatableText = new TranslatableText("enhanced.rarity.Legend");
                translatableText.formatted(Formatting.RED);
            }
        }
        return translatableText;
    }

    public static void increaseAttribute(MobEntity mobEntity, Rarity rarity) {
        List<EntityAttribute> Attributeslist = rarity.getAttributeslist();
        List<EntityAttributeModifier> AttributeModifierlist = rarity.getAttributeModifierlist();
        if (Attributeslist.size() == AttributeModifierlist.size()) {
            for (int i = 0; i < Attributeslist.size(); i++) {
                EntityAttributeModifier modifier = AttributeModifierlist.get(i);
                EntityAttributeInstance entityAttributeInstance = mobEntity.getAttributeInstance(Attributeslist.get(i));
                if (entityAttributeInstance == null) continue;
                if (!entityAttributeInstance.hasModifier(modifier))
                    entityAttributeInstance.addPersistentModifier(modifier);
            }
        }
    }

    public static void random(EnhancedBaseComponent component) {
        if (StartTest) {
            component.setRarity(Rarity.Good);
            return;
        }
        Random random = new Random();
        double value = random.nextDouble();
        if (value < 0.5) {
            value = random.nextDouble();
            if (value < 0.5) {
                value = random.nextDouble();
                if (value < 0.5) {
                    value = random.nextDouble();
                    if (value < 0.5) {
                        value = random.nextDouble();
                        if (value < 0.5) {
                            value = random.nextDouble();
                            if (value < 0.333) {
                                component.setRarity(Rarity.Legend);
                            } else component.setRarity(Rarity.Epic);
                        } else component.setRarity(Rarity.Ingenuity);
                    } else component.setRarity(Rarity.Excellent);
                } else component.setRarity(Rarity.Outstanding);
            } else component.setRarity(Rarity.Good);
        } else component.setRarity(Rarity.Ordinary);
    }

    public List<EntityAttribute> getAttributeslist() {
        return Attributeslist;
    }

    public List<EntityAttributeModifier> getAttributeModifierlist() {
        return AttributeModifierlist;
    }

    public Text getText() {
        return text;
    }
}
